<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by ajax.php
 * Date: 30.11.13
 * User: Lesh
 */

class Controller_Site_Ajax extends Controller
{
	public function before()
	{
		if( ! Request::initial()->is_ajax())
		{
			throw new HTTP_Exception_404();
		}
	}

	public function action_get_page()
	{
		$images = ORM::factory('image')->reset(false);

		$page = $this->page($this->request->post('page'));

		$images = $images->view_images($page['limit'], $page['offset']);
		$this->response->body(json_encode(array(
							'content' => View::factory('site/index/content', array(
											'images' => $images,
											'page' => $page['page'])
								)->render(),
						 )));
	}

	public function page($page)
	{
		$page = $page ? $page : 1;
		$limit = Kohana::$config->load('main')->get('page', 12);
		$offset = $limit * ($page - 1);
		return array('limit' => $limit, 'offset' => $offset, 'page' => $page);
	}
}